package com.passion.common.exception;

import org.apache.commons.lang3.StringUtils;

/**
 * 类描述：业务系统使用的自定义异常
 *
 * @author ZhuYin
 * @since 2024年05月07日 11:28
 */
public class MyException extends RuntimeException implements StatusCode {
    private static final long serialVersionUID = -13345478421L;

    protected Integer code;
    protected String msg;

    public MyException() {
        super();
    }

    public MyException(String msg) {
        super(msg);
        this.msg = msg;
        this.code = 500;
    }

    public MyException(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public MyException(String msg, Throwable cause) {
        super(msg, cause);
    }

    public MyException(Throwable cause) {
        super(cause);
    }

    /**
     * 获取状态码
     *
     * @return 返回状态码
     */
    @Override
    public Integer getCode() {
        return code;
    }

    @Override
    public String getMessage() {
        String message = super.getMessage();
        return StringUtils.isBlank(message) ? "服务器出错" : super.getMessage();
    }

    public MyException(StatusCode mc) {
        this.code = mc.getCode();
        this.msg = mc.getMessage();
    }

    /**
     * 默认方法
     *
     * @return 返回异常对象
     */
    @Override
    public MyException toException() {
        return StatusCode.super.toException();
    }
}
